package net.pws.oos.biz.model;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity
@Table(name = "s_user")
public class User extends Identifier {
	
	private static final long serialVersionUID = -724312178794549583L;

	private String name;
    
    private String account;
    
    private String mobile;
    
    private Boolean enabled = Boolean.TRUE;
    
    private String password;
    
    private String description;
    
    @Column(name = "privileged")
    private Boolean privileged = Boolean.FALSE;
    
    @Column(name = "mobile_access_allowed")
    private Boolean mobileAccessAllowed = Boolean.FALSE;
    
    @Column(name = "web_access_allowed")
    private Boolean webAccessAllowed = Boolean.FALSE;
    
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "organ_id")
    private Organization organization;
    
    @ManyToMany(mappedBy = "users", cascade = CascadeType.PERSIST)
    private Set<Role> roles = new HashSet<Role>();
    
    @Column(name = "has_deleted")
    private Boolean deleted = Boolean.FALSE;
    
    @Column(name = "source_id")
    private String sourceId;
    
    @Column(name = "source_system")
    private String sourceSystem;
    
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    public String getAccount() {
        return account;
    }
    
    public void setAccount(String account) {
        this.account = account;
    }
    
    public String getMobile() {
        return mobile;
    }
    
    public void setMobile(String mobile) {
        this.mobile = mobile;
    }
    
    public static boolean isEnabled(User user) {
        return (user.getEnabled() == null) ? false : user.getEnabled()
                                                         .booleanValue();
    }
    
    public Boolean getEnabled() {
        return enabled;
    }
    
    public void setEnabled(Boolean enabled) {
        this.enabled = enabled;
    }
    
    public String getPassword() {
        return password;
    }
    
    public void setPassword(String password) {
        this.password = password;
    }
    
    public String getDescription() {
        return description;
    }
    
    public void setDescription(String description) {
        this.description = description;
    }
    
    public static boolean hasPrivileged(User user) {
        if (user == null) {
            return false;
        }
        if (user.getPrivileged() == null) {
            return false;
        }
        return user.getPrivileged().booleanValue();
    }
    
    public Boolean getPrivileged() {
        return privileged;
    }
    
    public void setPrivileged(Boolean privileged) {
        this.privileged = privileged;
    }
    
    public Boolean getMobileAccessAllowed() {
        return mobileAccessAllowed;
    }
    
    public void setMobileAccessAllowed(Boolean mobileAccessAllowed) {
        this.mobileAccessAllowed = mobileAccessAllowed;
    }
    
    public Boolean getWebAccessAllowed() {
        return webAccessAllowed;
    }
    
    public void setWebAccessAllowed(Boolean webAccessAllowed) {
        this.webAccessAllowed = webAccessAllowed;
    }
    
    public Organization getOrganization() {
        return organization;
    }
    
    public void setOrganization(Organization organization) {
        this.organization = organization;
    }
    
    public Boolean getDeleted() {
        return deleted;
    }
    
    public void setDeleted(Boolean deleted) {
        this.deleted = deleted;
    }
    
    public Set<Role> getRoles() {
        return roles;
    }
    
    public void setRoles(Set<Role> roles) {
        this.roles = roles;
    }
    
    public String getSourceId() {
        return sourceId;
    }
    
    public void setSourceId(String sourceId) {
        this.sourceId = sourceId;
    }
    
    public String getSourceSystem() {
        return sourceSystem;
    }
    
    public void setSourceSystem(String sourceSystem) {
        this.sourceSystem = sourceSystem;
    }
    
}
